package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.IRoleDao;
import com.itheima.dao.Role;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = IRoleService.class)
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao roleDao;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findPageByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total,rows);
    }


    /**
     * 添加功能
     * @param role
     * @param permissionIds
     */
    @Override
    public void add(Role role, Integer[] permissionIds) {
        // 添加角色
        roleDao.add(role);

        // 建立角色与权限关联关系
        Integer roleId = role.getId();
        this.setPermissionAndRole(roleId,permissionIds);
    }

    // 删除功能
    @Override
    public void delete(Integer id) {

        // 删除权限与角色的关联
        roleDao.deleteAssociation(id);

        // 判断是否添加到用户
        long count = roleDao.findByRoleId(id);
        if (count > 0){
            throw new RuntimeException();
        }
        // 删除角色
        roleDao.delete(id);
    }

    // 通过ID查询
    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    // 编辑功能
    @Override
    public void edit(Role role, Integer[] permissionIds ) {
        // 编辑角色
        roleDao.edit(role);
        // 删除原角色和权限的关联
        roleDao.deleteAssociation(role.getId());
        // 建立新角色和权限的关联
        this.setPermissionAndRole(role.getId(),permissionIds);
    }

    // 获取所有角色
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    // 根据用户ID查找角色ID
    @Override
    public List<Integer> findRoleIdByUserId(Integer id) {
        return roleDao.findRoleIdByUserId(id);
    }

    public void setPermissionAndRole(Integer roleId,Integer[] permissionIds ){
        if (permissionIds != null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<String,Integer>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                roleDao.setPermissionAndRole(map);
            }
        }
    }
}
